<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 11.05.12
  Time: 18:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="db.DBConnection" %>
<%@ page import="db.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="db.Account" %>
<%
    int user_id = -1;
    String secret1 = "";
    Cookie[] cookies = request.getCookies();

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("user_id")) {
            user_id = Integer.parseInt(cookie.getValue());
        } else if (cookie.getName().equals("secret")) {
            secret1 = cookie.getValue();
        }
    }
    DBConnection dbConnection = new DBConnection();
    if (!dbConnection.isCookieOk(user_id, secret1)) {
        String redirectURL = "../index.jsp";
        response.sendRedirect(redirectURL);
    }

    int num = dbConnection.doGetNumberOfUnreadMessages(user_id);
    
    String login = request.getParameter("login");
    String password = request.getParameter("password");
    String role = request.getParameter("role");
    String firstName = request.getParameter("firstName");
    String lastName = request.getParameter("lastName");
    String otherName = request.getParameter("otherName");
    String sex = request.getParameter("sex");
    String birthDate = request.getParameter("birthDate");
    String birthPlace = request.getParameter("birthPlace");
    String country = request.getParameter("country");
    String city = request.getParameter("city");
    String address = request.getParameter("address");
    String postIndex = request.getParameter("postIndex");
    String passportNumber = request.getParameter("passportNumber");
    String passportPlace = request.getParameter("passportPlace");
    String passportDate = request.getParameter("passportDate");
    String note = request.getParameter("note");

    boolean isOk = false;
    boolean hasParams = false;
    if (login != null && firstName != null && lastName != null && otherName != null && sex != null
            && birthDate != null && birthPlace != null && country != null && city != null && address != null
            && postIndex != null  && passportNumber != null && passportPlace != null && passportDate != null) {
        hasParams = true;

        if (password == null) {

        } else {
            int s = -1;
            if (sex.equalsIgnoreCase("male")) {
                s = 1;
            } else {
                s = 0;
            }
            isOk = dbConnection.addUser(user_id, login, password, DBConnection.Role.valueOf(role), firstName, lastName, otherName, s,
                    birthDate, birthPlace, passportNumber, passportPlace, passportDate, country, city, address, postIndex, note);
        }
    }



%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Add user</title>
    <link rel="stylesheet" type="text/css" href="style"/>
</head>

<body>
<div class="logout" style="text-align:right;"><a href="../logout.jsp">  Logout</a></div>
<div class="header"><img src="logo.jpg"></div>
<div class="layout">
    <div class="sidebar">
        <ul class="cssmenu">
            <li><a href="personal_info.jsp">Personal info</a></li>
            <li><b><a href="operations.jsp">Operations</a></b></li>
            <li><a href="messages.jsp">Messages<% if (num != 0) {%> <b> (<%=num%>)</b> <%}%></a></li>
        </ul>
    </div>
    <form method="post" action="add_user.jsp">
        <div class="content">
            <% if (hasParams) {
                if (isOk) {
                    %> <p style="color: green;"> User added! </p> <%
                } else {
                    %> <p style="color: red;"> <%=dbConnection.lastOperationError%> </p><%
                }
            } %>
            <p>Personal info</p>
            <hr  width="500" align="left"/>
            <table class="table">
                <tr>
                    <td width="150">Login: <font color="red">*</font></td>
                    <td><input type="text" name="login"><td>
                    <td></td>
                </tr>
                <tr>
                    <td width="150">Password:</td>
                    <td><input type="password" name="password"><td>
                    <td></td>
                </tr>
                <td>Role: <font color="red">*</font></td>
                <td>
                    <select name="role">
                        <option> ADMIN </option>
                        <option> MODERATOR </option>
                    </select>
                </td>
                <td></td>
                <tr>
                    <td width="150">First name: <font color="red">*</font> </td>
                    <td><input type="text" name="firstName"><td>
                    <td></td>
                </tr>
                <tr>
                    <td>Last name: <font color="red">*</font></td>
                    <td><input type="text" name="lastName"><td>
                    <td></td>
                </tr>
                <tr>
                    <td>Other name:</td>
                    <td><input type="text" name="otherName"><td>
                    <td></td>
                </tr>
                <tr>
                    <td>Sex: <font color="red">*</font></td>
                    <td>
                        <select name="sex">
                            <option> male </option>
                            <option> female </option>
                        </select>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td>Date of birth: <font color="red">*</font></td>
                    <td><input type="text" name="birthDate"><td>
                    <td style="color: gray;"> YYYY-MM-DD </td>
                </tr>
                <tr>
                    <td>Place of birth: <font color="red">*</font></td>
                    <td><input type="text" name="birthPlace"><td>
                    <td style="color: gray"></td>
                </tr>
                <tr>
                    <td>Country: <font color="red">*</font></td>
                    <td><input type="text" name="country"><td>
                    <td style="color: gray"></td>
                </tr>
                <tr>
                    <td>City: <font color="red">*</font></td>
                    <td><input type="text" name="city"><td>
                    <td style="color: gray"></td>
                </tr>
                <tr>
                    <td>Address: <font color="red">*</font></td>
                    <td><input type="text" name="address"><td>
                    <td style="color: gray"></td>
                </tr>
                <tr>
                    <td>Post index: <font color="red">*</font></td>
                    <td><input type="text" name="postIndex"><td>
                    <td style="color: gray"></td>
                </tr>
            </table>
            <p>Certificate of identity</p>
            <hr width="500" align="left"/>
            <table class="table">
                <tr>
                    <td width="150">Number of passport: <font color="red">*</font></td>
                    <td><input type="text" name="passportNumber"><td>
                    <td style="color: gray"></td>
                </tr>
                <tr>
                    <td>Passport given by: <font color="red">*</font></td>
                    <td><input type="text" name="passportPlace"><td>
                    <td style="color: gray"></td>
                </tr>
                <tr>
                    <td>Date of issue: <font color="red">*</font></td>
                    <td><input type="text" name="passportDate"><td>
                    <td style="color: gray"> YYYY-MM-DD </td>
                </tr>

            </table>
            <hr width="500" align="left"/>
            <table class="table">
                <tr>
                    <td width="150">Note: </td>
                    <td><textarea name="note"></textarea></td>
                </tr>
            </table>
            <input type="submit" value="Add user">
        </div>

    </form>
</div>
</body>
</html>